
# -*- coding: utf-8 -*-
# 导入所需的模块
import random
import sys
import time

import pygame
from pygame.locals import *
# 使用pygame之前必须初始化
pygame.init()

# 设置主屏窗口大小
screen_long = 800
screen_wight = 600

screen = pygame.display.set_mode((screen_long,screen_wight))
# 设置窗口的标题，即游戏名称
pygame.display.set_caption('贪吃蛇---3.0版本')

# 第一屏画面
firstwhile = 0
while True:
    image = pygame.image.load('image\主背景.png')
    image_satart = pygame.image.load('image\开始游戏.png')
    image_end = pygame.image.load('image\退出游戏.png')
    image_puase = pygame.image.load('image\暂停.png')
    image_last = pygame.image.load('image\尾图.png')
    screen.fill((116,145,35))
    screen.blit(image,(0,0))
    screen.blit(image_satart,(222,245))
    screen.blit(image_end,(222,370))
    pygame.display.flip()
    for event in pygame.event.get():
        # 判断用户是否点了"X"关闭按钮,并执行if代码段
        if event.type == pygame.QUIT:
            # 卸载所有模块
            pygame.quit()
            # 终止程序，确保退出程序
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mx,my = event.pos
            if  222<=mx<=577 and 245<my<393:
                firstwhile = 1
                screen.blit(image, (0, 0))
                screen.blit(image_satart, (222 + 5, 245 + 5))
                pygame.display.flip()
                time.sleep(0.5)
                break
            elif 222<=mx<=577 and 370<my<518:
                if  222<=mx<=577 and 370<my<518:
                    screen.blit(image, (0, 0))
                    screen.blit(image_end, (222+5, 370+5))
                    pygame.display.flip()
                     # 卸载所有模块
                    pygame.quit()
                     # 终止程序，确保退出程序
                    sys.exit()
    if firstwhile == 1:
        firstwhile = 0
        break


bg_color = (46, 139, 87)  # 设置背景颜色

def derw_liine():
    for i in  range(0,screen_long,40):
        pygame.draw.line(screen,(0,0,0),(i,0),(i,screen_wight),1)
    for i in range(0, screen_wight, 40):
        pygame.draw.line(screen, (0, 0, 0), (0, i), (screen_long, i), 1)

screen.fill(bg_color)  # 每次循环都重绘屏幕

while True:
    snank_list = [(120,120)]
    snank_long = 1
    snank_color = (255,229,204)
    snank_headcolor = (0,191,255)
    snank_speed = [0,0]
    snank_time = 0
    snank_headx = 0
    snank_heady = 0
    snank_mspeed = 500



    food_color = (255,0,0)
    food_rect = [200,200,40,40]

    food_rect[0] = 40 * random.randint(0,screen_long/40-1)
    food_rect[1] *random.randint(0,screen_wight/40-1)

    FPSClock = pygame.time.Clock()

    snank_one = 1

    R = 0   # 主循环变量
    r = 0   # 次循环变量

    while True:
        # 循环获取事件，监听事件状态
        for event in pygame.event.get():
            # 判断用户是否点了"X"关闭按钮,并执行if代码段
            if event.type == pygame.QUIT:
                #卸载所有模块
                pygame.quit()
                #终止程序，确保退出程序
                sys.exit()
            elif event.type == KEYUP:
                if (event.key == pygame.K_d or event.key == pygame.K_RIGHT) and snank_one == 1:
                    if snank_speed[0] == 0:
                        snank_speed[0] = 40
                        snank_speed[1] = 0
                        snank_one = 0
                elif (event.key == pygame.K_a or event.key == pygame.K_LEFT) and snank_one == 1:
                    if snank_speed[0] == 0:
                        snank_speed[0] = -40
                        snank_speed[1] = 0
                        snank_one = 0
                elif (event.key == pygame.K_w or event.key == pygame.K_UP) and snank_one == 1:
                    if snank_speed[1] == 0:
                        snank_speed[0] = 0
                        snank_speed[1] = -40
                        snank_one = 0
                elif (event.key == pygame.K_s or event.key == pygame.K_DOWN)and snank_one == 1:
                    if snank_speed[1] == 0:
                        snank_speed[0] = 0
                        snank_speed[1] = 40
                        snank_one = 0
                # ESC暂停键
                elif event.key ==pygame.K_ESCAPE:
                    while True:
                        screen.blit(image_puase, (95, 100))
                        pygame.display.flip()
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                # 卸载所有模块
                                pygame.quit()
                                # 终止程序，确保退出程序
                            elif event.type == KEYUP:
                                if event.key == pygame.K_ESCAPE:
                                    r = 1
                                    break
                        if r==1:
                            r = 0
                            break

            # 吃食判定
        if (snank_list[0][0] == food_rect[0]) and (snank_list[0][1]== food_rect[1]):
            snank_long = snank_long + 1
            b = 1
            food_rect[0] = 40 * (random.randint(0, screen_long / 40 -1))
            food_rect[1] =40  * (random.randint(0, screen_wight / 40 -1))

        snank_time = snank_time + 1

        if len(snank_list) > 5: snank_mspeed = 400
        if len(snank_list) > 10: snank_mspeed = 330
        if len(snank_list) > 15: snank_mspeed = 250
        if len(snank_list) > 20: snank_mspeed = 150
        if len(snank_list) > 25: snank_mspeed = 100
        if snank_time > snank_mspeed:
            # 蛇头坐标更新
            snank_headx = snank_list[0][0] + snank_speed[0]
            snank_heady = snank_list[0][1] + snank_speed[1]
            snank_time = 1
            snank_one = 1

            snank_list.insert(0, (snank_headx, snank_heady))
            snank_list = snank_list[:snank_long]



        #print(snank_list)
        # 蛇身坐标更新
        screen.fill(bg_color)

        # 画食物
        pygame.draw.rect(screen, food_color, food_rect, 0,5)

        # 画蛇头
        pygame.draw.rect(screen, snank_headcolor, [snank_list[0][0],snank_list[0][1],40,40], 0, 5)
        # 画眼睛
        if snank_speed[0] == 40 or snank_speed[0] == -40:
            pygame.draw.rect(screen, (0,0,0),[snank_list[0][0] + 17,snank_list[0][1] + 6,6,6])
            pygame.draw.rect(screen, (0, 0, 0), [snank_list[0][0] + 17, snank_list[0][1] + 28, 6, 6])
        if snank_speed[1] == 40 or snank_speed[1] == -40:
            pygame.draw.rect(screen, (0, 0, 0), [snank_list[0][0] + 6, snank_list[0][1] + 17, 6, 6])
            pygame.draw.rect(screen, (0, 0, 0), [snank_list[0][0] + 28, snank_list[0][1] + 17, 6, 6])

        # 画蛇身
        m = 1
        while m < len(snank_list):
             pygame.draw.rect(screen, snank_color, [snank_list[m][0], snank_list[m][1], 40, 40], 0, 5)
             m = m+1

        # 自己吃自己死亡判定
        death = False
        if snank_long > 5:
            for p in range(len(snank_list)):
                if p>3:
                    if (snank_list[p] == snank_list[0]) and (snank_list[p] == snank_list[0]):
                        death = True


        if snank_list[0][0] == -40 or snank_list[0][0] == screen_long or snank_list[0][1]== -40 or snank_list[0][1] == screen_wight or death:
            while True:
                font = pygame.font.Font(None, 70)
                text1 = font.render(str(snank_long), True, (0, 0, 0))
                screen.blit(image_last, (0, 0))
                screen.blit(text1, (screen_long / 2 +90, screen_wight / 2 +5 ))

                pygame.display.flip()
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        # 卸载所有模块
                        pygame.quit()
                        # 终止程序，确保退出程序
                    elif event.type == KEYUP:
                        if event.key == pygame.K_r:
                            R = 1
                            break
                if R == 1:
                    break
        if R==1:
            break
        derw_liine()
        pygame.display.flip()  # 更新屏幕内容

